﻿using Lib.Web.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace PhotoGallery.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

        public FileResult GetVideo(string file = "")
        {
            return File(Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\App_Data", file), "video/mp4");
        }


        public FileResult GetImage(string file = "")
        {
            return File(Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\App_Data", file), "image/jpg");
        }

        public FileResult GetV(string file = "")
        {
            return File(Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\Uploads", file), "video/mp4");
        }

        //public RangeFileStreamResult Download(string file = "")
        //{
        //    var fileStream = new FileStream(Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\Uploads", file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        //    var mimeType = "video/mp4";
        //    string ext = Path.GetExtension(file);
        //    var fullFileName = Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\Uploads", file);
        //    var fileDownloadName = file;

        //    switch (ext)
        //    {
        //        case ".webm":
        //            mimeType = "video/webm";
        //            return new RangeFileStreamResult(fileStream, mimeType, file, DateTime.Now);
        //        case ".ogv":
        //            mimeType = "video/ogg";
        //            return new RangeFileStreamResult(fileStream, mimeType, file, DateTime.Now);
        //        default:
        //            mimeType = "video/mp4";
        //            return new RangeFileStreamResult(fileStream, mimeType, file, DateTime.Now);
        //    }
        //}

        //public ActionResult Video(string fileName)
        //{
        //    var pathBase = Server.MapPath("~/Downloads/Videos/");
        //    var filePath = pathBase + fileName;
        //    var contentType = ContentType(fileName);
        //    return new FilePathResult(filePath, contentType) { FileDownloadName = fileName };
        //}

        public ActionResult OceansClip(string type)
        {
            FileInfo oceansClipInfo = null;
            string oceansClipMimeType = String.Format("video/{0}", type);

            switch (type)
            {
                case "mp4":
                    oceansClipInfo = new FileInfo(Server.MapPath("~/Content/video/oceans-clip.mp4"));
                    break;
                case "webm":
                    oceansClipInfo = new FileInfo(Server.MapPath("~/Content/video/oceans-clip.webm"));
                    break;
                case "ogg":
                    oceansClipInfo = new FileInfo(Server.MapPath("~/Content/video/oceans-clip.ogv"));
                    break;
            }

            return new RangeFilePathResult(oceansClipMimeType, oceansClipInfo.FullName, oceansClipInfo.LastWriteTimeUtc, oceansClipInfo.Length);
            //return new RangeFileStreamResult(oceansClipInfo.OpenRead(), oceansClipMimeType, oceansClipInfo.Name, oceansClipInfo.LastWriteTimeUtc);
        }

        //public RangeFilePathResult Download(string file = "")
        //{
        //    //var fileStream = new FileStream(Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\Uploads", file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        //    //var mimeType = "video/mp4";
        //    string ext = Path.GetExtension(file);
        //    var fullFileName = Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\Uploads", file);
        //    var fileDownloadName = file;
        //    //FileInfo oceansClipInfo = new FileInfo(Path.Combine("D:\\_projects\\sip-mvc-gallery\\trunk\\PhotoGallery\\Uploads", file));
        //    string type;
        //    switch (ext)
        //    {
        //        case ".webm":
        //            type = "webm";
        //            break;
        //        case ".ogv":
        //            type = "ogg";
        //            break;
        //        default:
        //            type = "mp4";
        //            break;
        //    }

        //    FileInfo oceansClipInfo = null;
        //    string oceansClipMimeType = String.Format("video/{0}", type);

        //    switch (type)
        //    {
        //        case "mp4":
        //            oceansClipInfo = new FileInfo(fullFileName);
        //            break;
        //        case "webm":
        //            oceansClipInfo = new FileInfo(fullFileName);
        //            break;
        //        case "ogg":
        //            oceansClipInfo = new FileInfo(fullFileName);
        //            break;
        //    }
        //    return new RangeFilePathResult(oceansClipMimeType, oceansClipInfo.FullName, oceansClipInfo.LastWriteTimeUtc, oceansClipInfo.Length);
        //}

        public void GetThumbnail(string imageUrl)
        {
            int maxHeight = 180;
            int maxWidth = 180;

            WebImage img = new WebImage(imageUrl);
            int width = img.Width;
            int height = img.Height;

            int iStart;
            Decimal divider;
            int crop;

            bool portrait = (width < height);
            if (portrait)
            {
                iStart = width;
                divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                width = maxWidth;
                height = (int)Math.Round((Decimal)(height / divider));
                crop = (height - maxHeight) / 2;

                img.Resize(width, height, true, true)
                    .Crop(crop, 0, crop, 0)
                    .Write();
            }
            else
            {
                iStart = height;
                divider = Math.Abs((Decimal)iStart / (Decimal)maxHeight);
                height = maxHeight;
                width = (int)Math.Round((Decimal)(width / divider));
                crop = (width - maxWidth) / 2;

                img.Resize(width, height, true, true)
                    .Crop(0, crop, 0, crop)
                    .Write();
            }
        }
        
    }

    public class FileController : Controller
    {
        [Authorize]
        public ActionResult Get(string file)
        {
            return File(Path.Combine("", file),"video/mpeg");
        }
    }
}
